package com.manage.function.service.impl;

import java.util.List;

import com.manage.common.core.domain.entity.SysUser;
import com.manage.common.utils.SecurityUtils;
import com.manage.function.domain.FunctionAssetApplication;
import com.manage.function.domain.Review;
import com.manage.function.domain.vo.AssetCount;
import com.manage.function.domain.vo.AssetReview;
import com.manage.function.domain.vo.Constants;
import com.manage.function.mapper.FunctionAssetApplicationMapper;
import com.manage.system.domain.SysUserRole;
import com.manage.system.mapper.SysUserMapper;
import com.manage.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.manage.function.mapper.FunctionAssetMapper;
import com.manage.function.domain.FunctionAsset;
import com.manage.function.service.IFunctionAssetService;

import javax.annotation.Resource;

/**
 * 资产信息Service业务层处理
 *
 * @author jinghj
 * @date 2025-03-09
 */
@Service
public class FunctionAssetServiceImpl implements IFunctionAssetService
{
    @Autowired
    private FunctionAssetMapper functionAssetMapper;

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Resource
    private FunctionAssetApplicationMapper functionAssetApplicationMapper;
    /**
     * 查询资产信息
     *
     * @param id 资产信息主键;

    @Resource
    private FunctionAssetApplicationMapper  functionAssetApplicationMapper;

    /**
     * 查询资产信息
     *
     * @param id 资产信息主键
     * @return 资产信息
     */
    @Override
    public FunctionAsset selectFunctionAssetById(Long id)
    {
        return functionAssetMapper.selectFunctionAssetById(id);
    }

    /**
     * 查询资产信息列表
     *
     * @param functionAsset 资产信息
     * @return 资产信息
     */
    @Override
    public List<FunctionAsset> selectFunctionAssetList(FunctionAsset functionAsset)
    {

        return functionAssetMapper.selectFunctionAssetList(functionAsset);
    }

    /**
     * 新增资产信息
     *
     * @param functionAsset 资产信息
     * @return 结果
     */
    @Override
    public int insertFunctionAsset(FunctionAsset functionAsset)
    {
        return functionAssetMapper.insertFunctionAsset(functionAsset);
    }

    /**
     * 修改资产信息
     *
     * @param functionAsset 资产信息
     * @return 结果
     */
    @Override
    public int updateFunctionAsset(FunctionAsset functionAsset)
    {
        return functionAssetMapper.updateFunctionAsset(functionAsset);
    }

    /**
     * 批量删除资产信息
     *
     * @param ids 需要删除的资产信息主键
     * @return 结果
     */
    @Override
    public int deleteFunctionAssetByIds(Long[] ids)
    {
        return functionAssetMapper.deleteFunctionAssetByIds(ids);
    }

    /**
     * 删除资产信息信息
     *
     * @param id 资产信息主键
     * @return 结果
     */
    @Override
    public int deleteFunctionAssetById(Long id)
    {
        return functionAssetMapper.deleteFunctionAssetById(id);
    }


    // 资产申请审核
    @Override
    public boolean assetReview(AssetReview assetReview) {
        List<FunctionAsset> functionAssets = functionAssetMapper.selectAssetCounts();
        for (FunctionAsset functionAsset : functionAssets) {
            if (assetReview.getzcId() == functionAsset.getId()){
                if (assetReview.getShuliang()>functionAsset.getQuantity()){
                    throw new SecurityException("资产数量不足");
                }
            }
        }
        String username = SecurityUtils.getUsername();
        SysUser sysUser = sysUserMapper.selectUserByUserName(username);
        Long userId = sysUser.getUserId();
        SysUserRole sysUserRole = sysUserRoleMapper.selectSysUserRoleByUserId(userId);
        // 非管理员申请,其实这个判断角色是多余的，因为前台直接控制就可以，但是为了安全起见还是加了
        if (sysUserRole.getRoleId()!=1){
            FunctionAssetApplication functionAssetApplication = new FunctionAssetApplication();
            functionAssetApplication.setUserId(userId);
            functionAssetApplication.setAssetId(assetReview.getzcId());
            functionAssetApplication.setDeptId(sysUser.getDeptId());
            functionAssetApplication.setQuantity(assetReview.getShuliang());
            functionAssetApplication.setReason(assetReview.getReason());
            functionAssetApplication.setStatus(Constants.ADMIN_IN_PROGRESS_STATUS);
            functionAssetApplication.setProgress(Constants.ADMIN_IN_PROGRESS);
            functionAssetApplicationMapper.insertFunctionAssetApplication(functionAssetApplication);
        }
        FunctionAsset functionAsset = new FunctionAsset();
        //资产总数
        for (FunctionAsset asset : functionAssets) {
             if (asset.getId() == assetReview.getzcId()) {
                 Long quantity = asset.getQuantity();
                 Long shuliang = assetReview.getShuliang();
                 Long newQuantity = quantity-shuliang;
                 functionAsset.setId(assetReview.getzcId());
                 functionAsset.setQuantity(newQuantity);
                 functionAssetMapper.updateFunctionAsset(functionAsset);
             }
        }
        return true;
    }
}
